package com.m4f.city.business.domain;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.NotPersistent;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.GrantedAuthorityImpl;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.oauth2.provider.ClientDetails;

import com.m4f.city.business.domain.ifc.IModelDomain;
import com.m4f.city.utils.serializer.annotation.M4FPropertySerializable;
import com.m4f.city.utils.serializer.annotation.M4FSerializable;

@PersistenceCapable(identityType = IdentityType.APPLICATION, detachable="true")
@M4FSerializable(name = "application")
public class Application implements ClientDetails, UserDetails, IModelDomain {
	@PrimaryKey  
	@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
	@M4FPropertySerializable(name="id")
	private Long id;
	
	@Persistent
	@M4FPropertySerializable(name="name")
	private String name;
	
	@Persistent
	@M4FPropertySerializable(name="description")
	private String description;
	
	@Persistent
	private Long companyId;
	
	// TODO LOGO
	
	@Persistent
	@M4FPropertySerializable(name="uri")
	private String applicationUri;
	
	@Persistent
	private String clientId;
	
	@Persistent
	private String clientSecret;
	
	@Persistent
	private Boolean webApp;
	
	@Persistent
	private String webServerRedirectUri;
	
	@NotPersistent //TODO ??
	private List<String> authorizedGrantTypes = new ArrayList<String>();

	@NotPersistent //TODO ??
	private List<GrantedAuthority> authorities= Arrays.asList(new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_APPLICATION")});
	
	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public Long getCompanyId() {
		return companyId;
	}

	public void setCompanyId(Long companyId) {
		this.companyId = companyId;
	}

	public void setAuthorities(List<GrantedAuthority> authorities) {
		this.authorities = authorities;
	}

	public String getClientId() {
		return clientId;
	}

	public void setClientId(String clientId) {
		this.clientId = clientId;
	}

	public String getClientSecret() {
		return clientSecret;
	}

	public void setClientSecret(String clientSecret) {
		this.clientSecret = clientSecret;
	}

	public String getWebServerRedirectUri() {
		return webServerRedirectUri;
	}

	public void setWebServerRedirectUri(String webServerRedirectUri) {
		this.webServerRedirectUri = webServerRedirectUri;
	}

	@Override
	public List<GrantedAuthority> getAuthorities() {
		return new ArrayList<GrantedAuthority>(); // TODO
	}

	@Override
	public List<String> getAuthorizedGrantTypes() {
		return this.authorizedGrantTypes;
	}
	
	public void setAuthorizedGrantTypes(List<String> authorizedGrantTypes) {
		this.authorizedGrantTypes = authorizedGrantTypes; 
	}

	@Override
	public List<String> getScope() {
		return new ArrayList<String>(); // TODO 
	}

	@Override
	public boolean isScoped() { // TODO
		return false;
	}

	@Override
	public boolean isSecretRequired() { // TODO
		return true;
	}

	public String getApplicationUri() {
		return applicationUri;
	}

	public void setApplicationUri(String applicationUri) {
		this.applicationUri = applicationUri;
	}

	@Override
	public String getPassword() {
		return this.clientSecret;
	}

	@Override
	public String getUsername() {
		return this.clientId;
	}

	@Override
	public boolean isAccountNonExpired() {
		return true;
	}

	@Override
	public boolean isAccountNonLocked() {
		return true;
	}

	@Override
	public boolean isCredentialsNonExpired() {
		return true;
	}

	@Override
	public boolean isEnabled() {
		return true;
	}

	public Boolean getWebApp() {
		return webApp;
	}

	public void setWebApp(Boolean webApp) {
		this.webApp = webApp;
	}
}
